package com.yuncheng.spcyApi.gxca;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.spcyApi.constant.ca.GxCaConstant;
import com.yuncheng.spcyApi.entity.SpcySqsxWjjl;
import com.yuncheng.spcyApi.service.ISpcySqsxWjjlService;
import com.yuncheng.spcyApi.utils.HnCaUtils;
import com.yuncheng.vo.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "审评查验ca接口")
@RestController
@RequestMapping("/api/spzx-spcy/gxca")
public class gxca {
	private static final Logger log = LoggerFactory.getLogger(gxca.class);

	@Resource
	@Lazy
	private gxcaService service;

	@Resource
	@Lazy
	private ISpcySqsxWjjlService spcySqsxWjjlService;

	@Resource
	private HnCaUtils hnCaUtils;

//	public static String dzqzServerUrl = "https://172.18.0.100/yjjcaess/gxyjj/v1/signature"; // 正式
//	public static String dzqzServerUrl = "http://114.115.181.136:9232/yjjcaess/gxyjj/v1/signature";
//	public static String dzqzServerUrl = "http://172.18.0.11:8080/yjjcaess/gxyjj/v1/signature"; // 内网

	@AutoLog(value = "获取签章-通过序列号")
	@ApiOperation(value = "获取签章-通过序列号")
	@GetMapping(value = "/getSealList")
	public HttpResult<?> getSealList(@RequestParam("certSn")String certSn) throws Exception {
		Map result = new HashMap<>();
		result.put("state", 1);
		result.put("sealList", service.getSealList(certSn));

		return HttpResult.ok(result);
	}

	// 构造json并对json进行排序
	@AutoLog(value = "对签章文件进行排序")
	@ApiOperation(value = "对签章文件进行排序")
	@GetMapping(value = "sortJson")
	public HttpResult<?> sortJson(@RequestParam("certSn")String certSn,
								  @RequestParam("ids")String ids,
								  @RequestParam(value = "seal",defaultValue = "")String seal,
								  @RequestParam(value = "CMA_flag",defaultValue = "false")boolean CMA_flag,
								  @RequestParam(value = "CNAS_flag",defaultValue = "false")boolean CNAS_flag,
								  @RequestParam(value = "SYZ",defaultValue = "false")boolean SYZ,
								  @RequestParam(value = "QFZ",defaultValue = "false")boolean QFZ,
								  @RequestParam(value = "SQQZR",defaultValue = "false")boolean SQQZR
						 ) throws Exception {
		String data = "";
		String jsonStr = "";
		Map result = new HashMap<>();
		HashMap<String,Object> buffer = new HashMap<>();
		buffer.put("seal",seal);  // 签章名称
		buffer.put("CMA_flag",CMA_flag); // CMA 关键字
		buffer.put("CNAS_flag",CNAS_flag); // CNAS 关键字
		buffer.put("SYZ",SYZ); // 首页章
		buffer.put("QFZ",QFZ); // 骑缝章
		buffer.put("SQQZR",SQQZR); // 个人签章

		List sealList = service.getSealList(certSn);
		for (String id : ids.split(",|，")) {
			SpcySqsxWjjl wjjl = spcySqsxWjjlService.getById(id);

			jsonStr = service.createJSONdata(wjjl,certSn, sealList, buffer);
			com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(jsonStr);
//			data += (data == "" ? "" : "、") + JSONSortHelper.getSortJson(jsonObject).toString();
			data += (data == "" ? "" : "、") + JSONSortHelper.getSortJson(jsonObject);

		}
		log.info(data);
		// /\\/g
		result.put("data", data);

		return HttpResult.ok(result);
	}

	@AutoLog(value = "对签章文件进行盖章")
	@ApiOperation(value = "对签章文件进行盖章")
	@PostMapping(value = "dzbgQz")
	public HttpResult<?> dzbgQz(@RequestBody Map<String,String> dataMap) throws Exception {
//		@RequestParam("ids")String ids,
//		@RequestParam("data")String data,
//		@RequestParam("signinfo")String signinfo
		String ids = dataMap.get("ids");
		String data = dataMap.get("data");
		String signinfo = dataMap.get("signinfo");

		String qzurl = GxCaConstant.dzqzServerUrl;
		JSONObject object = new JSONObject();
		Map result = new HashMap<>();
		//定义接收数据
		JSONObject json = new JSONObject();


		json.put("data", data);
		json.put("signInfo", signinfo);
		String jsonStr = json.toString();
		com.alibaba.fastjson.JSONObject jsonObject = service.dzbgQz(qzurl, jsonStr);

		object.put("msg", jsonObject.getString("message"));
		// 如果签章成功，自动下载pdf到服务器本地
		log.info("jsonData",jsonObject);
		if (jsonObject.containsKey("data")) {
			String downloadurl = jsonObject.get("data").toString();
			object = service.downloadDzbgQz(downloadurl, ids, object);
			object.put("state", 1);
		} else {
			object.put("state", 0);
			if (jsonObject.containsKey("code")) {
				object.put("code", jsonObject.get("code").toString());
			}
		}

		result.put("jsonData",jsonObject);
		return HttpResult.ok(result);
	}

	@GetMapping(value = "testSort")
	public HttpResult<?> testSort(String jsonStr){
		com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(jsonStr);
		JSONObject sortJson = JSONSortHelper.getSortJson(jsonObject);
		return HttpResult.ok(sortJson);
	}

	@AutoLog(value = "对Pdf文件盖章-通过文件id")
	@ApiOperation(value = "对Pdf文件盖章-通过文件id")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(name = "id",value = "文件id,可选择多个文件id参数")
	})
	@GetMapping(value = "pdfFileHcCa")
	public HttpResult<?> pdfFileHcCa(@RequestParam("id") String id){
		if (StringUtils.isBlank(id)){
			return HttpResult.error("文件参数不能为空");
		}
		List<String> idsList = Arrays.asList(id.split(",|，"));
		for (String fid : idsList) {
			HttpResult<?> httpResult = hnCaUtils.fileCaByGJZ(fid);// 文件盖章
			if (!httpResult.isSuccess()){
				return HttpResult.error(httpResult.getMessage());
			}
		}

		return HttpResult.ok("盖章成功");
	}

}
